package com.yxq.mdt.app.modules.withdraw.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author L
 * @time
 * @describe 提现-手机号码验证
 */
public class VerificationPhoneActivity extends BaseActivity {


    @BindView(R.id.login_et_account)
    EditText loginEtAccount;
    @BindView(R.id.login_et_name)
    EditText loginEtName;
    @BindView(R.id.login_tv_code)
    TextView loginTvCode;
    @BindView(R.id.draw_btn_submit)
    Button drawBtnSubmit;
    CountDownTimer countDownTimer;

    @Override
    public int getLayoutId() {
        return R.layout.activity_verification_phone;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    @OnClick({R.id.login_tv_code, R.id.draw_btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_tv_code:
                sms();
                break;
            case R.id.draw_btn_submit:
                submit();
                break;
        }
    }

    private void submit() {
        if (loginEtAccount.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入手机号码");
            return;
        }
        if (!StringUtils.isMobileExact(loginEtAccount.getText().toString())) {
            ToastUtils.showShort("请输入合法的手机号码");
            return;
        }
        if (loginEtName.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入验证码");
            return;
        }
        Intent data = new Intent();
        data.putExtra("phone", loginEtAccount.getText().toString());
        setResult(RESULT_OK, data);
        finish();
    }

    private void sms() {
        if (loginEtAccount.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入手机号码");
            return;
        }
        if (!StringUtils.isMobileExact(loginEtAccount.getText().toString())) {
            ToastUtils.showShort("请输入合法的手机号码");
            return;
        }
    }

    private void stopTimer() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    private void timer() {
        stopTimer();
        loginTvCode.setEnabled(false);
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                loginTvCode.setText((l / 1000) + "s");
            }

            @Override
            public void onFinish() {
                loginTvCode.setText("获取验证码");
                loginTvCode.setEnabled(true);
            }
        };
        countDownTimer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopTimer();
    }
}
